#define TBLUTIL_C


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <project.h>
/** /#define DEBUG_TBLUTIL*/
//#ifdef DEBUG_TBLUTIL
#define DEBUG_95
//#endif

#include <define.h>
#include <transact.h>
#include <bdt.h>
#include <cdt.h>
#include <cst.h>
#include <hdt.h>
#include <iit.h>
#include <lct.h>
#include <mht.h>
#include <mit.h>
#include <tct.h>
#include <entry.h>
#include <file.h>
#include <logo.h>
#include <msg.h>
#include <msgfuncs.h>
#include <operate.h>
#include <print.h>
#include <tblutil.h>
#include <table.h>
#include <ui.h>
#include <spdebug.h>


int inGetMITIndex(void) {
    return(inGetMerchantNum() - 1);
}

void vdGetCDTFileName(char* szFileName) {
    char szTmpFileName[FILENAME_SIZE + 1];

    /* get correct CDT text file based on Merchant number */
    sprintf(szTmpFileName, "C%02d.DAT", inGetMerchantNum());

    if (inChkFileExist(szTmpFileName) == VS_SUCCESS)
        strcpy(szFileName, szTmpFileName);
    else
        strcpy(szFileName, CDT_FILENAME);
}

void vdGetCSTFileName(char* szFileName) {
    char szTmpFileName[FILENAME_SIZE + 1];

    /* get correct CST text file based on Merchant number */
    sprintf(szTmpFileName, "S%02d.DAT", inGetMerchantNum());

    if (inChkFileExist(szTmpFileName) == VS_SUCCESS)
        strcpy(szFileName, szTmpFileName);
    else
        strcpy(szFileName, CST_FILENAME);
}


int inIncOfflineCount(void) {
    long lnOfflineCount;

    lnOfflineCount = lnGetNbrOfOfflineTran();

    vdSetNbrOfOfflineTran(++lnOfflineCount);

    if (inSaveBDTRec(inGetBatchIndex()) != VS_SUCCESS) {
        vdDisplayMessageFromFile(BDT_ERR_MSG, ERROR_MSG);
        return(VS_ERR);
    }

    return(VS_SUCCESS);
}


/*-------------------------------------------------------------------------
    Function:       inDecOfflineCount
    Description:    Decrement the Offline transaction count of the BDT
    Parameters:
    Returns:        VS_ERR, VS_SUCCESS
    Notes:
--------------------------------------------------------------------------*/
int inDecOfflineCount(void) {
    long lnOfflineCount;

    lnOfflineCount = lnGetNbrOfOfflineTran();

    if (lnOfflineCount > 0L)
        lnOfflineCount--;

    vdSetNbrOfOfflineTran(lnOfflineCount);

    if (inSaveBDTRec(inGetBatchIndex()) != VS_SUCCESS) {
        vdDisplayMessageFromFile(BDT_ERR_MSG, ERROR_MSG);
        return(VS_ERR);
    }

    return(VS_SUCCESS);
}

/*-------------------------------------------------------------------------
    Function:       inResetOfflineCount
    Description:    Reset the Offline transaction count of the BDT
    Parameters:
    Returns:        VS_ERR, VS_SUCCESS
    Notes:
--------------------------------------------------------------------------*/
int inResetOfflineCount(void) {
    vdSetNbrOfOfflineTran(0L);

    if (inSaveBDTRec(inGetBatchIndex()) != VS_SUCCESS) {
        vdDisplayMessageFromFile(BDT_ERR_MSG, ERROR_MSG);
        return(VS_ERR);
    }

    return(VS_SUCCESS);
}




/*********************************************************************************************
* @func int | inLoadGetCDTIndex |
*
*       Finds CDT Index based on account number (pchPan).
*       Duplicate entries produce a pick list for selection of appropriate range.
*
*       Notes : On file errors returns corresponding error codes, return -1
*               (ie VS_ERR) if invalid card/card not found, on success returns
*               returns a value >= 0, the corresponding CDT index.
*
*           NB: The card number is passed in as a NULL terminated string;
*               The full card number is passed but only the first CARD_RANGE_SIZE
*               characters are used for comparison of the range.
*
*
*       FIELD ID:       VALIDATE_MANUAL_NO       0x0044
*
* @parm TRANSACTION_OBJECT *    | pobTran   |
* @parm char *  | pchPan    |
*
* @rdesc Return values.
* @flag int    |  VS_SUCCESS    |
* @flag int    |  VS_ERR        |
* @end
**********************************************************************************************/

int inLoadGetCDTIndex(TRANSACTION_OBJECT* pobTran,char* pchPan) {
#ifndef IMAM_REMOVE
    signed int inResult;
    signed int inRetVal;
    int inRecIndex;
    int inNumberOfMatches;
    int inRecNumArray[4];
    char szChoiceMsg[MESSAGE_SIZE + 1];
    int inIssuer;
    long lnMsgNum;

    pdebug(("--inLoadGetCDTIndex--"));


    vdSGCurCDTIndex(-1);
    inRetVal = VS_ERR;
    inRecIndex = 0;

    inNumberOfMatches = 0;
    inIssuer = 0;
    memset(szChoiceMsg, 0x00, sizeof(szChoiceMsg));

    if (strlen(pchPan) == 0)
        return(VS_ERR);
    pdebug(("--%s    %s--", pchPan, szGetPANLo()));

    do {
        inResult = inLoadCDTRec(inRecIndex);
        if (inResult == VS_ERR)
            break;

        /* Check by Acct Ranging */
        pdebug(("--%s    %s--", pchPan, szGetPANLo()));
        if ((strncmp(pchPan, szGetPANLo(), min(strlen(pchPan), strlen(szGetPANLo()))) >= 0) && (strncmp(pchPan, szGetPANHi(), min(strlen(pchPan), strlen(szGetPANHi()))) <= 0)) {
            if (pobTran->fInteracDebit) {
                inRetVal = inRecIndex;
                break;
            }

            if (!(fGetDebitFlag() == VS_FALSE && inGetType() == DEBIT_CARD) && !(!fGetManEntry() && pobTran->srBRec.fManual == VS_TRUE)) {
                if ((inGetType() == DEBIT_CARD) || (inGetType() == EBT_CARD) || (inGetIssuerNum() != inIssuer)) {

                    if (inGetType() != DEBIT_CARD && inGetType() != EBT_CARD)
                        inIssuer = inGetIssuerNum();
			
                    inRecNumArray[inNumberOfMatches++] = inRecIndex;

                    if (inNumberOfMatches > 1)
                        szChoiceMsg[strlen(szChoiceMsg)] = '~';

                    switch (inGetType()) {
                    case DEBIT_CARD:
                        lnMsgNum = DEBIT_CHOICE_MSG5x;
                        break;
                    case EBT_CARD:
                        lnMsgNum = EBT_CHOICE_MSG5x;
                        break;
                    case PURCHASE_CARD:
                        lnMsgNum = PURCHASE_CHOICE_MSG5x;
                        break;
#ifdef FLEET_DEFINED
                    case FLEET_CARD:
                        lnMsgNum = FLEET_CHOICE_MSG5x;
                        break;
#endif
                    default:
                        lnMsgNum = CREDIT_CHOICE_MSG5x;
                        break;
                    }
                    vdGetMessageFromFile(lnMsgNum, &szChoiceMsg[strlen(szChoiceMsg)]);
                    /*** out with the old
                            strcpy( &szChoiceMsg[ strlen( szChoiceMsg )],
                                srCDTRec.szSCardLabel );
                                ***/

                    if (inNumberOfMatches > 3)
                        break;
                }
            }
        }

        inRecIndex++;
    } while (1);

    /*if (close(hCDTFHandle) < 0)
        inRetVal = VS_ERR;*/

    if (inNumberOfMatches == 1)
        inRetVal = inRecNumArray[0];
    else if (inNumberOfMatches > 1) {
        // vdClearNonTitleLines();
        vdClearASCII8x21Window(1, 3, 21, 8);

#ifdef CREDIT_DEBIT_OPTION_DISP
        inRetVal = obDisp.inDO_ChooseOne(NULL, pchGetMsg(DEBIT_CHOOSE_CARD_MSG), szChoiceMsg, inNumberOfMatches, NULL_PFI);
#else
	inRetVal = 1;
#endif  /* CREDIT_DEBIT_OPTION_DISP  */

        if (inRetVal == VS_ESCAPE)
            return(VS_ESCAPE);
        inRetVal = inRecNumArray[inRetVal - 1];
    }

    /* return value is important, not != VS_SUCCESS */
    if (inRetVal < 0)
        vdResetCDTRec();
    else if (inRetVal >= 0) {
        inLoadCDTRec(inRetVal);
        vdSGCurCDTIndex(inRetVal);  /* Remember the current CDT that is loaded */

        /* Get the CDT also for card labels */
        inGetIssuerRecord(inGetIssuerNum());
    }
    return(inRetVal);
#endif //IMAM_REMOVE
	return (VS_SUCCESS);
}

/*********************************************************************************************
* @func int | inGetCDTTotRecs |
*
*       Finds number of records in the CDT data file.
*
* @parm VOID
*
* @rdesc Return values.
* @flag int    |  Number of records in the CDT data file    |
* @end
**********************************************************************************************/

int inGetCDTTotRecs(void) {
    long lnSize;
    char szFileName[FILENAME_SIZE + 1];

    vdGetCDTFileName(szFileName);
    lnSize = lnGetFileSize(szFileName);
    return((int) ((lnSize - GEN_VER_SIZE) / (long) SIZE_CDT_REC));
}

/*********************************************************************************************
* @func int | inGetCSTTotRecs |
*
*       Finds number of records in the CST data file.
*
* @parm VOID
*
* @rdesc Return values.
* @flag int    |  Number of records in the CST data file    |
* @end
**********************************************************************************************/

int inGetCSTTotRecs() {
    long lnSize;
    long lnRSize;
    char szFileName[FILENAME_SIZE + 1];

    /**/pdebug(("--inGetCSTTotRecs--"));/**/

    vdGetCSTFileName(szFileName);
    lnSize = lnGetFileSize(szFileName);
    lnRSize = (long) SIZE_CST_REC;
    lnSize = (lnSize - GEN_VER_SIZE) / lnRSize;
    return((int) lnSize);
}

int inLoadHostData(int inHostRecNo) {
    signed int inRetVal;

    if ((inRetVal = inLoadHDTRec(inHostRecNo)) == VS_SUCCESS) {
        /* AS1 use host index for record number */
        inRetVal = inLoadMHTRec(inGetBatchIndex());
    }

    return (inRetVal);
}

int inSaveHostData(int inHostRecNo) {
    int inRetVal;

    if ((inRetVal = inSaveHDTRec(inHostRecNo)) == VS_SUCCESS)
        inRetVal = inSaveMHTRec(inGetBatchIndex());

    return(inRetVal);
}

/*********************************************************************************************
* @func int | inGetHDTTotRecs |
*
*       Finds number of records in the HDT data file.
*
* @parm VOID
*
* @rdesc Return values.
* @flag int    |  Number of records in the HDT data file    |
* @end
**********************************************************************************************/

int inGetHDTTotRecs() {
    long lnSize;

    /** /pdebug(("--inGetHDTTotRecs--"));*/

    lnSize = lnGetFileSize(HDT_FILENAME);
    return((int) ((lnSize - GEN_VER_SIZE) / (long) SIZE_HDT_REC));
}

/*********************************************************************************************
* @func int | inGetIITTotRecs |
*
*       Finds number of records in the IIT data file.
*
* @parm VOID
*
* @rdesc Return values.
* @flag int    |  Number of records in the IIT data file    |
* @end
**********************************************************************************************/

int inGetIITTotRecs() {
    long lnSize;

    /** /pdebug(("--inGetIITTotRecs--"));*/

    lnSize = lnGetFileSize(IIT_FILENAME);
    return((int) ((lnSize - GEN_VER_SIZE) / (long) SIZE_IIT_REC));
}


int inGetIssuerRecord(int inIssuerNumber) {
    int inRec = 0;
    do {
        if (inLoadIITRec(inRec) == VS_ERR) {
            vdSGErrorMessage(IIT_ERR_MSG21);
            return(VS_ERR);
        }
        inRec++;
    } while (inIssuerNumber != inGetIssuerNumber());

    return(VS_SUCCESS);
}


/*-------------------------------------------------------------------------
    Function :  inGetDefaultLCT()
    Description : Get the record that corresponds to the transaction LCT index
    Parameters : n/a
    Returns : VS_ERR, VS_SUCCESS
    Globals : 
    Notes : 
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      0   08/18/98 Page_K1    Creation...
--------------------------------------------------------------------------*/
int inGetDefaultLCT() {
    return(inLoadLCTRec(inGetLanguageIdx()));
}        

/*-------------------------------------------------------------------------
    Function :  inGetReceiptLCT()
    Description : Get the record that corresponds to the transaction LCT index
    Parameters : n/a
    Returns : VS_ERR, VS_SUCCESS
    Globals : 
    Notes : 
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      0   08/18/98 Page_K1    Creation...
--------------------------------------------------------------------------*/
int inGetReceiptLCT(TRANSACTION_OBJECT* pobTran) {
    /* Load language record */
    if (inLoadLCTRec(pobTran->srBRec.inLCTIndex) != VS_SUCCESS)
        return (VS_ERR);
    return(VS_SUCCESS);
}        

/*-------------------------------------------------------------------------
    Function :  inGetDefaultTemplateName()
    Description : Get Default Template file name
    Parameters : n/a
    Returns : VS_ERR, VS_SUCCESS
    Globals : 
    Notes : 
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      0   08/18/98 Page_K1    Creation...
--------------------------------------------------------------------------*/
int inGetDefaultTemplateName(char* pszTemplateFileName) {
    inGetDefaultLCT();
    strcpy(pszTemplateFileName, STATIC_DATA_PATH);
    strcat(pszTemplateFileName, szGetLanguageNamePrefix());
    strcat(pszTemplateFileName, szGetTemplateFileNameSuffix());

    return(VS_SUCCESS);
}        

/*-------------------------------------------------------------------------
    Function :  inGetReport1Template()
    Description : Set the report template to the active template
    Parameters : n/a
    Returns : VS_ERR, VS_SUCCESS
    Globals : 
    Notes : 
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      0   08/18/98 Page_K1    Creation...
--------------------------------------------------------------------------*/
int inGetReport1Template() {
    char szTemplateFileName[FILENAME_SIZE + 1];

    strcpy(szTemplateFileName, STATIC_DATA_PATH);
    strcat(szTemplateFileName, szGetReport1NamePrefix());
    strcat(szTemplateFileName, szGetTemplateFileNameSuffix());


    if (inSwitchTemplate(szTemplateFileName) < 0) {
        vdDisplayMessageFromFile(FORMATER_ERR_MSG, ERROR_MSG);
        return(VS_ERR);
    }
    return(VS_SUCCESS);
}        

/*-------------------------------------------------------------------------
    Function :  inGetReport2Template()
    Description : Set the report template to the active template
    Parameters : n/a
    Returns : VS_ERR, VS_SUCCESS
    Globals : 
    Notes : 
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      0   08/18/98 Page_K1    Creation...
--------------------------------------------------------------------------*/
int inGetReport2Template() {
    char szTemplateFileName[FILENAME_SIZE + 1];

    strcpy(szTemplateFileName, STATIC_DATA_PATH);
    strcat(szTemplateFileName, szGetReport2NamePrefix());
    strcat(szTemplateFileName, szGetTemplateFileNameSuffix());


    if (inSwitchTemplate(szTemplateFileName) < 0) {
        vdDisplayMessageFromFile(FORMATER_ERR_MSG, ERROR_MSG);
        return(VS_ERR);
    }
    return(VS_SUCCESS);
}        

/*-------------------------------------------------------------------------
    Function :  inGetReceiptTemplate()
    Description : If INTERAC, set the template using the pobTran language field
                  to read the LCT record and get the name for the template(The
                  language field was set in GET_CARD_FIELDS. If not INTERAC 
                  then check TCT PromptLanguage flag and prompt for language 
                  if set. If prompt flag not set use the pobTran language field
                  which was set to the default value in inInitOper().
    Parameters : n/a
    Returns : VS_ERR, VS_SUCCESS
    Globals : 
    Notes : 
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      0   08/18/98 Page_K1    Creation...
--------------------------------------------------------------------------*/
int inGetReceiptTemplate(TRANSACTION_OBJECT* pobTran) {
    char szTemplateFileName[FILENAME_SIZE + 1];

    if (fGetPromptLanguage() && !pobTran->fLanguageOnSwipe && pobTran->srBRec.inPrintOption != PRT_NONE) {
        /* prompt for the desired receipt language and set pobTran field */
        if (inGetLanguageIndex(pobTran) == VS_ESCAPE)
            return (VS_ESCAPE);
    }

    /* Load language specified by pobTran->srBRec.inLCTIndex */
    inGetReceiptLCT(pobTran);

    strcpy(szTemplateFileName, STATIC_DATA_PATH);
    strcat(szTemplateFileName, szGetLanguageNamePrefix());
    strcat(szTemplateFileName, szGetTemplateFileNameSuffix());

    if (inSwitchTemplate(szTemplateFileName) < 0) {
        vdDisplayMessageFromFile(FORMATER_ERR_MSG, ERROR_MSG);
        return(VS_ERR);
    }

    return(VS_SUCCESS);
}        

int inGetLanguageIndex(TRANSACTION_OBJECT* pobTran) {
    signed int inResult;
    int inNumChoices;
    char szChoiceMsg[MESSAGE_SIZE + 1];
    char szFileNameBuffer[MESSAGE_SIZE + 1];
    char* chNull = 0;

    inNumChoices = inBuildLCTPrompt(szChoiceMsg);
    if (inNumChoices == 1)
        pobTran->srBRec.inLCTIndex = 0;
    else {
        /* pdebug(("MSG=%s %d", szChoiceMsg, inNumChoices)); */
        vdClearNonTitleLines();
        vdDisplayAt(obDisp.inHomeCol, obDisp.inSecondLineCOne, pchGetMsg(RECEIPT_LANGUAGE_MSG), NO_CLEAR);
        vdSetEntryFont(VS_FALSE);
        inResult = obDisp.inDO_ChooseOne(chNull, chNull, szChoiceMsg, inNumChoices, NULL_PFI);
        vdResetEntryFont(VS_FALSE);

        /* pdebug(("RET=%d", inResult)); */
        if (inResult == VS_ESCAPE)
            return(VS_ESCAPE);

        if (inResult > 0) {
            pobTran->srBRec.inLCTIndex = inResult - 1;
            inLoadLCTRec(pobTran->srBRec.inLCTIndex);
            memset(szFileNameBuffer, 0, MSG_FILE_NAME_SIZE);
            strcpy(szFileNameBuffer, "f:");    
            strcat(szFileNameBuffer, szGetLanguageNamePrefix());    
            strcat(szFileNameBuffer, "msg.dat");

            if (open(szFileNameBuffer, O_RDONLY) < 0) {
                vdDisplayMessage("Pls Dnld Span Files", WARNING_MSG);
                return(VS_ERR);
            }
        }
    }

    return (VS_SUCCESS);
}

/*-------------------------------------------------------------------------
    Function :  inSetDefaultLanguage()
    Description : Sets the batch record inLCTIndex to the TCT inLanguageIndex
    Parameters : n/a
    Returns : VS_ERR, VS_SUCCESS
    Globals : 
    Notes : 
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      0   08/31/98 Page_K1    Creation...
--------------------------------------------------------------------------*/
int inSetDefaultLanguage(TRANSACTION_OBJECT* pobTran) {
    pobTran->srBRec.inLCTIndex = inGetLanguageIdx();
    return(VS_SUCCESS);
}        

/*-------------------------------------------------------------------------
    Function :  inGetLCTTotRecs()
    Description : Determines the number of records in the LCT file
    Parameters : n/a
    Returns : Number of records.
    Globals : 
    Notes : 
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      0   09/01/98 Page_K1    Creation...
--------------------------------------------------------------------------*/
int inGetLCTTotRecs() {
    long lnSize;

    /** dprintf(("--inGetLCTTotRecs--"));*/

    lnSize = lnGetFileSize(LCT_FILENAME);
    return((int) ((lnSize - GEN_VER_SIZE) / (long) SIZE_LCT_REC));
}

/*-------------------------------------------------------------------------
    Function :  inSetLCTTemplateSuffix()
    Description : Sets the field szTemplateFileNameSuffix in the LCT file
                    to value determined by the printer type
    Parameters : n/a
    Returns : Number of records.
    Globals : 
    Notes : 
    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
      0   09/01/98 Page_K1    Creation...
--------------------------------------------------------------------------*/
int inSetLCTTemplateSuffix(char* pszSuffix) {
    int inLCTCount,
    inCount;

    inLCTCount = inGetLCTTotRecs();
    for (inCount = 0; inCount < inLCTCount; inCount++) {
        /* Load language record */
        if (inLoadLCTRec(inCount) != VS_SUCCESS) {
            vdDisplayMessageFromFile(LCT_ERR_MSG, ERROR_MSG);
            return(VS_ERROR);
        }

        vdSetTemplateFileNameSuffix(pszSuffix);
        if (inSaveLCTRec(inCount) != VS_SUCCESS) {
            vdDisplayMessageFromFile(LCT_ERR_MSG, ERROR_MSG);
            return(VS_ERROR);
        }
    }
    return(VS_SUCCESS);
}


/*-------------------------------------------------------------------------
    Function : inBuildLCTPrompt
    Description : Finds CDT Index based on acct number
    Parameters : char * - Acct Number or Card Label
    Returns : Index or Error
    Globals :
    Notes : On file errors returns corresponding error codes, return -1
            (ie VS_ERR) if invalid card/card not found, on success returns
            returns a value >= 0, the corresponding CDT index.

            NB: The card number is passed in as a NULL_CH terminated string;
            The full card number is passed but only the first CARD_RANGE_SIZE
            characters are used for comparison of the range

    Modification History :
      #     Date      Who                     Description
    ----- -------- ---------- ---------------------------------------------
--------------------------------------------------------------------------*/
int inBuildLCTPrompt(char* szChoiceMsg) {
    int inLCTCount,
    inCount;
    LCT_REC srLCTLocalRec;

    memset(szChoiceMsg, '\0', MESSAGE_SIZE + 1);
    inLCTCount = inGetLCTTotRecs();
    /* pdebug(("Count=%d", inLCTCount)); */
    for (inCount = 0; inCount < inLCTCount; inCount++) {
        /* Load language record */
        if (inLoadLCTRecFromStruct(inCount, &srLCTLocalRec) != VS_SUCCESS) {
            /* pdebug(("RetCount=%d", inCount)); */
            return(inCount);
        }
        if (inCount > 0)
            szChoiceMsg[strlen(szChoiceMsg)] = '~';
        strcat(szChoiceMsg, szGetLanguageNameFontBytesFromStruct(&srLCTLocalRec));
        /* pdebug(("Build=%s", szChoiceMsg)); */
        if (inCount > 4)  // __vip_0103_ changed from 2 to 4
            break;
    }
    return (inCount);
}

/*********************************************************************************************
* @func void | vdGetMHTFileName |
*
*       get correct MHT text file based on Merchant number
*
* @parm char * |  szFileName    |
*
* @rdesc VOID
* @end
**********************************************************************************************/

void vdGetMHTFileName(char* szFileName) {
    char szTmpFileName[FILENAME_SIZE + 1];

    /* get correct MHT text file based on Merchant number */
    sprintf(szTmpFileName, "M%02d.DAT", inGetMerchantNum());

    if (inChkFileExist(szTmpFileName) == VS_SUCCESS)
        strcpy(szFileName, szTmpFileName);
    else
        strcpy(szFileName, MHT_FILENAME);
}

/*-------------------------------------------------------------------------
    Function:       inIncSTAN
    Description:    Increment the STAN of the MHT
    Parameters:
    Returns:        VS_ERR, VS_SUCCESS
    Notes:
--------------------------------------------------------------------------*/
int inIncSTAN(void) {
    long lnSTAN;
    TRANSACTION_OBJECT* pobTran;

    pdebug(("--inIncSTAN--"));

    if ((pobTran = pstGet_pobTran()) == NULL_CH)
        return VS_ERR;

    lnSTAN = lnGetSTAN();

    if (++lnSTAN > lnMaxValue(inGetSTANSize()))
        lnSTAN = 1L;

    vdSetSTAN(lnSTAN);

    return (inSaveMHTRec(inGetBatchIndex()));
}

/*-------------------------------------------------------------------------
    Function:       inDecSTAN
    Description:    Decrement the STAN of the MHT
    Parameters:
    Returns:        VS_ERR, VS_SUCCESS
    Notes:
--------------------------------------------------------------------------*/

int inDecSTAN(void) {
    long lnSTAN;
    TRANSACTION_OBJECT* pobTran;

    if ((pobTran = pstGet_pobTran()) == NULL_CH)
        return VS_ERR;

    lnSTAN = lnGetSTAN();

    if (lnSTAN == 0L)
        lnSTAN = lnMaxValue(inGetSTANSize());
    else
        lnSTAN--;

    vdSetSTAN(lnSTAN);

    return (inSaveMHTRec(inGetBatchIndex()));
}


/*-------------------------------------------------------------------------
    Function:       inIncHostInvNum
    Description:    Increment the Host Inv number of the MHT
    Parameters:
    Returns:        VS_ERR, VS_SUCCESS
    Notes:
--------------------------------------------------------------------------*/

int inIncHostInvNum(void) {
    int inHostInvNum;
    TRANSACTION_OBJECT* pobTran;

    pdebug(("--inIncHostInvNum--"));

    if ((pobTran = pstGet_pobTran()) == NULL_CH)
        return VS_ERR;

    inHostInvNum = shGetHostInvNum();

    if ((long) inHostInvNum++ > lnMaxValue(SEQ_NUM_SIZE))
        inHostInvNum = 1;

    vdSetHostInvNum(inHostInvNum);

    return (inSaveMHTRec(inGetBatchIndex()));
}

/*-------------------------------------------------------------------------
    Function : inResetSeqNum
    Description : Reset and save the Sequence Number to the MHT.
    Parameters : void
    Returns : int - VB_ERR, VB_SUCCESS
    Globals :
    Notes :

--------------------------------------------------------------------------*/
int inResetSeqNum(void) {
    TRANSACTION_OBJECT* pobTran;

    if ((pobTran = pstGet_pobTran()) == NULL_CH)
        return VS_ERR;

    /** /pdebug(("--inResetSeqNum--"));*/

    vdSetHostInvNum(0);

    return (inSaveMHTRec(inGetBatchIndex()));
}

int inGetMHTTotRecs() {
    long lnSize;
    char szFileName[FILENAME_SIZE + 1];

    pdebug(("--inGetMHTTotRecs--"));


    vdGetMHTFileName(szFileName);
    lnSize = lnGetFileSize(szFileName);
    return((int) ((lnSize - GEN_VER_SIZE) / (long) SIZE_MHT_REC));
}

int inGetMITTotRecs(void) {
    long lnSize;

    //pdebug(("--inGetMITTotRecs--"));

    lnSize = lnGetFileSize(MIT_FILENAME);
    return((int) ((lnSize - GEN_VER_SIZE) / (long) SIZE_MIT_REC));
}

#undef TBLUTIL_C

